package co.yixiang.yshop.module.shop.service.canvasdetail;

import cn.hutool.core.util.ObjectUtil;
import co.yixiang.yshop.module.shop.controller.admin.canvas.vo.CanvasDetailReqVO;
import co.yixiang.yshop.module.shop.controller.admin.canvas.vo.CanvasJsonUpdateReqVO;
import co.yixiang.yshop.module.shop.controller.admin.canvasdetail.vo.CanvasDetailRespVO;
import co.yixiang.yshop.module.shop.controller.app.canvas.vo.CanvasEnableReqVO;
import co.yixiang.yshop.module.shop.convert.canvasdetail.CanvasDetailConvert;
import co.yixiang.yshop.module.shop.dal.dataobject.canvas.CanvasDO;
import co.yixiang.yshop.module.shop.dal.dataobject.canvasdetail.CanvasDetailDO;
import co.yixiang.yshop.module.shop.dal.mysql.canvas.CanvasMapper;
import co.yixiang.yshop.module.shop.dal.mysql.canvasdetail.CanvasDetailMapper;
import co.yixiang.yshop.module.shop.enums.canvas.CanvasTerminalEnum;
import co.yixiang.yshop.module.shop.enums.common.StateEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 画布明细 Service 实现类
 *
 * @author moxiangrong
 */
@Service
@Validated
public class CanvasDetailServiceImpl implements CanvasDetailService {

    @Resource
    private CanvasDetailMapper canvasDetailMapper;

    @Resource
    private CanvasMapper canvasMapper;

    @Override
    public List<CanvasDetailRespVO> getListByCanvasId(Long canvasId) {
        return CanvasDetailConvert.INSTANCE.convertList(canvasDetailMapper
                .selectList(new LambdaQueryWrapper<CanvasDetailDO>().eq(CanvasDetailDO::getCanvasId, canvasId)));
    }

    @Override
    public CanvasDetailRespVO getCanvasDetail(CanvasDetailReqVO reqVO) {
        return CanvasDetailConvert.INSTANCE.convert(canvasDetailMapper
                .selectOne(new LambdaQueryWrapper<CanvasDetailDO>()
                        .eq(CanvasDetailDO::getCanvasId, reqVO.getId())
                        .eq(CanvasDetailDO::getTerminal, reqVO.getTerminal())
                        .last("limit 1")));
    }

    @Override
    public void batchInsert(Long canvasId) {
        List<CanvasDetailDO> insertDOS = new ArrayList<>();
        CanvasDetailDO mini = new CanvasDetailDO();
        mini.setCanvasId(canvasId);
        mini.setJson("");
        mini.setTerminal(CanvasTerminalEnum.MINI.getValue());
        insertDOS.add(mini);
        CanvasDetailDO h5 = new CanvasDetailDO();
        h5.setCanvasId(canvasId);
        h5.setJson("");
        h5.setTerminal(CanvasTerminalEnum.H5.getValue());
        insertDOS.add(h5);
        CanvasDetailDO app = new CanvasDetailDO();
        app.setCanvasId(canvasId);
        app.setJson("");
        app.setTerminal(CanvasTerminalEnum.APP.getValue());
        insertDOS.add(app);
        canvasDetailMapper.insertBatch(insertDOS);
    }

    @Override
    public void updateJson(CanvasJsonUpdateReqVO vo) {
        canvasDetailMapper.update(null, new LambdaUpdateWrapper<CanvasDetailDO>()
                .eq(CanvasDetailDO::getCanvasId, vo.getId())
                .eq(CanvasDetailDO::getTerminal, vo.getTerminal())
                .set(CanvasDetailDO::getJson, vo.getJson())
        );
    }

    @Override
    public void batchDelete(Long canvasId) {
        canvasDetailMapper.delete(new LambdaQueryWrapper<CanvasDetailDO>().eq(CanvasDetailDO::getCanvasId, canvasId));
    }

    @Override
    public CanvasDetailRespVO getCanvasJson(CanvasEnableReqVO reqVO) {
        Long id = reqVO.getId();
        Integer type = reqVO.getType();
        CanvasDO canvasDO = canvasMapper.selectOne(new LambdaQueryWrapper<CanvasDO>()
                .eq(ObjectUtil.isNotNull(id), CanvasDO::getId, id)
                .eq(ObjectUtil.isNotNull(type), CanvasDO::getType, type)
                .eq(ObjectUtil.isNull(id),CanvasDO::getIsIndex, StateEnum.OPEN.getValue())
                .eq(CanvasDO::getState, StateEnum.OPEN.getValue()).last("limit 1")
        );
        if (ObjectUtil.isNull(canvasDO)) {
            return null;
        }
        CanvasDetailDO canvasDetailDO = canvasDetailMapper.selectOne(
                new LambdaQueryWrapper<CanvasDetailDO>()
                .eq(CanvasDetailDO::getCanvasId, canvasDO.getId())
                .eq(CanvasDetailDO::getTerminal, reqVO.getTerminal()).last("limit 1")
        );
        CanvasDetailRespVO result = CanvasDetailConvert.INSTANCE.convert(canvasDetailDO);
        result.setName(canvasDO.getName());
        return result;
    }
}
